const Service = require("egg").Service;

class ToExamineService extends Service {

    async getList() {
        const { ctx, app } = this;
        const {
            pageIndex,
            pageSize,
            p,
            n,
            s,
            keyword, state
        } = ctx.request.body;
        let nb = n === "市辖区" ? "" : n;
        let address = p + nb + s;
        let sql = "select * from toExamine where id=id "
        if (address) {
            sql += `and address like "%${address}%" `
        }
        if (keyword) {
            sql += `and name like "%${keyword}%" `
        }
        if (state !== null) {
            sql += `and state=${state * 1} `
        }
        let res = await app.mysql.query(sql);
        let data = res.slice((pageIndex - 1) * pageSize, pageIndex * pageSize);
        console.log((pageIndex - 1) * pageSize, pageIndex * pageSize);
        return {
            total: res.length,
            data
        }
    }
    //删除
    async del() {
        const { ctx, app } = this;
        const { id } = JSON.parse(ctx.query.params);
        let res = await app.mysql.delete("toExamine", { id });
        return res;
    }
    //添加
    async add() {
        const { ctx, app } = this;
        let {
            name,
            address,
            agent,
            state,
            price,
            active
        } = ctx.request.body;
        let address1 = address.p + (address.n === "市辖区" ? "" : address.n) + address.s;
        let res = await app.mysql.insert(active === 0 ? "roomList" : "roomLease", {
            name,
            address: address1,
            agent,
            state,
            price,
            update_time: Date.now()
        })
        return res
    }
    //编辑
    async edit() {
        const { ctx, app } = this;
        let {
            name,
            title,
            state,
            HowBigM2,
            sum,
            id,
            isSetState
        } = ctx.request.body;
        let res = null;
        if (isSetState) {
            res = await app.mysql.update("toExamine", { id, state });
        } else {
            res = await app.mysql.update("toExamine", {
                name,
                title,
                state,
                HowBigM2,
                sum,
                id,
                update_time: Date.now()
            })
        }

        return res
    }
}
module.exports = ToExamineService;